package tuvienna.jade.table;

import jade.core.AID;
import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;

import java.io.IOException;

import tuvienna.jade.Constants;
import tuvienna.jade.InterestSet;

/*
 * A bit different now, this behavior periodically
 *  (and whenever the discussion composition changes)
 *  informs all Agents what they were talking about
 */
 
public class DiscussionBehavior extends OneShotBehaviour {

	private static final long serialVersionUID = 6620920478723225336L;
	private TableAgent table;

	public DiscussionBehavior(TableAgent table) {
		this.table = table;
	}
	
	public void action() {
		ACLMessage interestsMsg = new ACLMessage(ACLMessage.INFORM);
		InterestSet commonInterests = table.getInterestsOfParticipants();
		for (AID participant:table.getParticipants()) {
			interestsMsg.addReceiver(participant);
		}
		interestsMsg.setConversationId(Constants.KNOWLEDGE_SHARED);
		//printParticipants();
		try {
			interestsMsg.setContentObject(commonInterests);
			table.send(interestsMsg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void printParticipants() {
		System.out.print(table.getLocalName()+": {");
		for (AID aid:table.getParticipants()) {
			System.out.print(aid.getLocalName()+",");
		}
		System.out.println("}");
	}
	
}
